package demo.gaobo.com.myapplication.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import butterknife.Bind;
import demo.gaobo.com.myapplication.Constants;
import demo.gaobo.com.myapplication.R;
import demo.gaobo.com.myapplication.base.BaseActivity;
import demo.gaobo.com.myapplication.base.BasePresenter;
import demo.gaobo.com.myapplication.fragment.HomeFragment;
import demo.gaobo.com.myapplication.fragment.MineFragment;
import demo.gaobo.com.myapplication.fragment.WorkFragment;

public class MallHomeActivity extends BaseActivity {


    @Bind(R.id.home_content)
    FrameLayout homeContent;
    @Bind(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    @Bind(R.id.layout_main)
    LinearLayout layoutMain;
    private int lastSelectedPosition = 0;
    private HomeFragment mHomeFragment;
    private MineFragment mMineFragment;
    private WorkFragment mWorkFragment;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_mall_home;
    }

    @Override
    protected String setToolBarTitle() {
        return "首页";
    }


    @Override
    public void initView() {
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        // TODO 设置背景色样式
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setBarBackgroundColor(R.color.background_gray_color);


        //init fragment
        initFragment(0);
    }

    @Override
    public void initListener() {
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.tab_home_icon, "主页").setActiveColorResource(R.color.colorAccent).setInactiveIconResource(R.mipmap.tab_home_icon_def).setInActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.mipmap.tab_works_icon, "购物车").setActiveColorResource(R.color.colorAccent).setInactiveIconResource(R.mipmap.tab_works_icon_def).setInActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.mipmap.tab_course_icon, "我的").setActiveColorResource(R.color.colorAccent).setInactiveIconResource(R.mipmap.tab_course_icon_def).setInActiveColorResource(R.color.colorPrimary))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                initFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    private void initFragment(int i) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.home_content, mHomeFragment, Constants.HOME_TAG);
                } else {
                    fragmentTransaction.show(mHomeFragment);
                }
                break;

            case 1:
                if (mWorkFragment == null) {
                    mWorkFragment = new WorkFragment();
                    fragmentTransaction.add(R.id.home_content, mWorkFragment, Constants.WORK_TAG);
                } else {
                    fragmentTransaction.show(mWorkFragment);
                }
                break;

            case 2:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.home_content, mMineFragment, Constants.MINE_TAG);
                } else {
                    fragmentTransaction.show(mMineFragment);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();

    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (mHomeFragment != null) {
            fragmentTransaction.hide(mHomeFragment);
        }

        if (mWorkFragment != null) {
            fragmentTransaction.hide(mWorkFragment);
        }

        if (mMineFragment != null) {
            fragmentTransaction.hide(mMineFragment);
        }
    }
}
